package com.nassir.chatroom.frame;

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import com.nassir.chatroom.engine.ClientEngine;
import com.nassir.chatroom.util.FrameUtils;
import com.nassir.chatroom.util.Logs;

/**
 * 登录窗体
 * 
 * @author nassir.wen
 * @date 2012-12-3下午5:40:14
 * @Copyright: Copyright (c) 2012
 */
@SuppressWarnings("serial")
public class LoginFrame extends JFrame {

    private JLabel chatNameLabel = new JLabel("大名:");
    private JTextField chatName = new JTextField();
    private JButton loginBtn = new JButton("连接"); 
    private JButton exitBtn = new JButton("退出");
    
    public static Color bg = new Color(150, 181, 230);

    public LoginFrame() {
        super("聊天室入口");
        init();
        initBtnClick();
    }

    /**
     * 初始化
     */
    private void init() {
        Container c = this.getContentPane();
        c.setLayout(null);
        FrameUtils.setCenter(this);

        chatName.setEditable(true);
        chatName.setBounds(80, 20, 200, 30);

        Font f = new Font("宋体", Font.BOLD, 25);
        chatName.setFont(f);

        chatNameLabel.setBounds(10, 20, 100, 30);
        chatNameLabel.setFont(f);

        c.add(chatNameLabel);
        c.add(chatName);

        loginBtn.setBounds(50, 60, 60, 30);
        exitBtn.setBounds(150, 60, 60, 30);

        c.add(loginBtn);
        c.add(exitBtn);

        c.setBackground(bg);
        this.setSize(300, 160);
        this.setVisible(true);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
    }
    
    /**
     * 初始化按钮事件
     */
    private void initBtnClick(){
        loginBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                ClientEngine.login(chatName.getText());
                new MainFrame();
                Logs.info("login...");
            }
        });
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {

                }
            }
        });

        exitBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Logs.info("exit...");
                System.exit(0);
            }
        });
    }
}
